/**************************************************************
* Copyright (C) 2022 www.eipflow.com 孙泽伟版权所有(盗版必究)
*
* 作者: 孙泽伟(QQ 1039318332)
* 创建时间: 2018/11/9 9:21:04
* 文件名: 
* 描述: 
* 
* 修改历史
* 修改人：
* 时间：
* 修改说明：
*
**************************************************************/
namespace EIP.System.Logic.Impl
{
    /// <summary>
    /// 
    /// </summary>
    public class SystemZeroCodeGridCustomLogic : DapperAsyncLogic<SystemZeroCodeGridCustom>, ISystemZeroCodeGridCustomLogic
    {
        #region 构造函数
        private readonly ISystemZeroCodeGridCustomRepository _systemZeroCodeGridCustomRepository;
        /// <summary>
        /// 模块
        /// </summary>
        /// <param name="zeroCodeRepository"></param>
        public SystemZeroCodeGridCustomLogic(ISystemZeroCodeGridCustomRepository systemZeroCodeGridCustomRepository)
        {
            _systemZeroCodeGridCustomRepository = systemZeroCodeGridCustomRepository;
        }

        #endregion

        #region 方法

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="input">实体</param>
        /// <returns></returns>
        public async Task<OperateStatus> Save(SystemZeroCodeGridCustom input)
        {
            var currentUser = EipHttpContext.CurrentUser();
            OperateStatus operateStatus = new OperateStatus();
            SystemZeroCodeGridCustom zeroCode = await FindAsync(f => f.CreateUserId == currentUser.UserId && f.Key == input.Key);
            if (zeroCode == null)
            {
                input.CreateTime = DateTime.Now;
                input.CreateUserId = currentUser.UserId;
                input.CreateUserName = currentUser.Name;
                input.UpdateTime = DateTime.Now;
                input.UpdateUserId = currentUser.UserId;
                input.UpdateUserName = currentUser.Name;
                input.ZeroCodeGridCustomId = CombUtil.NewComb();
                operateStatus = await InsertAsync(input);
            }
            else
            {
                zeroCode.Config = input.Config;
                zeroCode.Title = input.Title;
                zeroCode.UpdateTime = DateTime.Now;
                zeroCode.UpdateUserId = currentUser.UserId;
                zeroCode.UpdateUserName = currentUser.Name;

                operateStatus = await UpdateAsync(zeroCode);
            }
            if (operateStatus.Code == ResultCode.Success)
            {
                operateStatus.Msg = "保存成功";
            }
            return operateStatus;
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="paging"></param>
        /// <returns></returns>
        public async Task<OperateStatus<PagedResults<SystemZeroCodeGridCustomFindOutput>>> Find(SystemZeroCodeGridCustomFindInput paging)
        {
            return OperateStatus<PagedResults<SystemZeroCodeGridCustomFindOutput>>.Success(await _systemZeroCodeGridCustomRepository.Find(paging));
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="paging"></param>
        /// <returns></returns>
        public async Task<OperateStatus<SystemZeroCodeGridCustom>> FindById(IdInput input)
        {
            var data = await FindAsync(f => f.ZeroCodeGridCustomId == input.Id);
            return OperateStatus<SystemZeroCodeGridCustom>.Success(data);
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="paging"></param>
        /// <returns></returns>
        public async Task<OperateStatus<SystemZeroCodeGridCustomFindByKeyOutput>> FindByKey(IdInput<string> input)
        {
            var currentUser = EipHttpContext.CurrentUser();
            SystemZeroCodeGridCustomFindByKeyOutput output = new SystemZeroCodeGridCustomFindByKeyOutput();
            using (var fix = new SqlDatabaseFixture())
            {
                var data = await fix.Db.SystemZeroCodeGridCustom.SetSelect(s => new { s.Config,s.CreateUserName,s.CreateUserId }).FindAllAsync(f => f.Key == input.Id && (f.CreateUserId == currentUser.UserId || f.CreateUserName == "超级管理员"));
                if (data != null)
                {
                    var dataFind = data.ToList().FirstOrDefault(f => f.CreateUserId == currentUser.UserId);
                    if (dataFind == null)
                    {
                        dataFind = data.ToList().FirstOrDefault(f => f.CreateUserName == "超级管理员");
                    }
                    if (dataFind != null)
                    {
                        output.Config = dataFind.Config;
                    }
                }
            }
            return OperateStatus<SystemZeroCodeGridCustomFindByKeyOutput>.Success(output);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<OperateStatus> Delete(IdInput<string> input)
        {
            foreach (var id in input.Id.Split(','))
            {
                var gridCustomId = Guid.Parse(id);
                await DeleteAsync(u => u.ZeroCodeGridCustomId == gridCustomId);
            }
            return OperateStatus.Success();
        }

        #endregion
    }
}